/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kungkhies.mvc.kungkhie.upload;

import com.kungkhies.jms.kungkhie.upload.KungkherKungkhie;
import com.kungkhies.jms.kungkhie.upload.KungkhieUploadEnqueueService;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StringMultipartFileEditor;

/**
 *
 * @author clayton
 */
@Controller
public class KungkhieUploadController
{
    private KungkhieUploadEnqueueService kungkhieUploadEnqueueService;
    
    @Autowired
    public KungkhieUploadController(KungkhieUploadEnqueueService kungkhieUploadEnqueueService)
    {
        this.kungkhieUploadEnqueueService =   kungkhieUploadEnqueueService;
    }
    
    @RequestMapping(value = "/uploadKungkhie", method = RequestMethod.POST)
    public String handleFormUpload(@RequestParam("kungkher") String kungkher,
            @RequestParam("kungkhieFile") MultipartFile file, Map<String, Object> model)
    {
        if (!isRegistered(kungkher))
        {
            model.put("kungkherName", kungkher);
            model.put("uploadErrorMessage", "unregisterd user");
            
            return "uploadFailure";         
        }
        
        if (!isSignedIn(kungkher))
        {
            model.put("kungkherName", kungkher);
            model.put("uploadErrorMessage", "user not signed in");
            
            return "uploadFailure";         
        }
        
        byte[] bytes = null;
        if (!file.isEmpty())
        {
            try
            {
                bytes = file.getBytes();
            }
            catch (IOException ex)
            {
                Logger.getLogger(KungkhieUploadController.class.getName()).log(Level.SEVERE, null, ex);
                
                model.put("uploadErrorMessage", "The cause of the upload failure is not known. Please re-submit.\n\nIf the problem persists please contack the helpline at support@kungkhies.com.");
                
                
                return "uploadFailure";
            }
            
            //Until kungkher = principal.username is in form
            if (kungkher == null || kungkher.trim().equals(""))
            {
                kungkher = "kungkher";
            }
            
            KungkherKungkhie kk = new KungkherKungkhie(kungkher, new String(bytes));
            
            kungkhieUploadEnqueueService.enqueueKungkhieUpload(kungkher, new String(bytes));
            
            model.put("kungkhieData", new String(bytes));
            
            return "uploadSuccess";
        }
        else
        {
            model.put("uploadErrorMessage", "Upload file is empty.");
            
            return "uploadFailure";
        }
    }

    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
    }
    
    public boolean isRegistered(String kungkherName)
    {
        return true;
    }
    
    public boolean isSignedIn(String kungkherName)
    {
        return true;
    }
}
